package nl.utwente.fmt.vertalerbouw.tarrh;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.lang.reflect.InvocationTargetException;

import org.antlr.runtime.RecognitionException;
import org.apache.commons.io.FilenameUtils;

/**
 * Application that compiles a Tarrh source file and executes the resulting java
 * bytecode.
 */
public class TarrhInterpreter {
	/**
	 * Application that compiles a Tarrh source file and executes the resulting
	 * java bytecode. Strictly not implemented as an interpreter, but behaves
	 * identically to a proper interpreter.
	 * 
	 * @param args
	 *            the arguments to the application.
	 * @throws IOException
	 *             if the specified file cannot be read.
	 * @throws RecognitionException
	 *             if an error occurs during parsing, checking or encoding.
	 * @throws InvocationTargetException
	 *             if an error occurs during execution.
	 */
	public static void main(String[] args) throws IOException, RecognitionException, InvocationTargetException {
		File inputFile = null;
		if (args.length == 1 && !args[0].startsWith("-")) {
			inputFile = new File(args[0]);
		} else {
			System.err.println("usage: TarrhInterpreter source-file");
			System.exit(1);
		}
		Reader inputReader = new FileReader(inputFile);
		String className = FilenameUtils.removeExtension(inputFile.getName());
		TarrhUtils.encode(className, inputReader).execute();
		inputReader.close();
	}
}
